package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.Department;
import com.three.entity.Staff;
import com.three.mapper.DepartmentMapper;
import com.three.mapper.StaffMapper;
import com.three.service.DepartmentService;
import com.three.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    //获取所有部门名
    @Override
    public R getAll() {
        return R.ok(departmentMapper.getAll());
    }

    //获取部门信息
    @Override
    public R getAllDept(Integer currentPage, Integer pageSize, Department department) {
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        List<Department> list = departmentMapper.getAllDept(department);

        PageInfo<Department> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    //添加部门
    @Override
    public R addDept(Department department) {
        department.setCounts(0);//初始化员工人数为0
        int i = departmentMapper.insertSelective(department);
        return R.ok();

    }

    //修改部门信息
    @Override
    public R updateDept(Department department) {
        int i = departmentMapper.updateByPrimaryKeySelective(department);
        if (i > 0) {
            return R.ok("修改成功");
        }
        return R.error("修改失败");
    }

    //删除部门
    @Override
    public R deleteDept(Integer deptid) {
        //判断该部门下是否有员工
        int a = departmentMapper.selectByDeptidCount(deptid);
        if (a > 0) {
            return R.error("该部门下还有员工，无法删除");
        }else{
        int i = departmentMapper.deleteByDeptid(deptid);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error("删除失败");
        }
    }
    }

}
